package com.tcl.shoplink.wheel;

import java.util.ArrayList;
import java.util.Arrays;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.sholink.wheel.adapters.AbstractWheelTextAdapter;
import com.shoplink.launcher.R;

public class WheelSpinnerView extends WheelView {
	private Drawable mLeftDrawable = getResources().getDrawable(
			R.drawable.arrow_left);
	private Drawable mRightDrawable = getResources().getDrawable(
			R.drawable.arrow_right);;

	private String[] mType;

	private boolean mVisibleArrow = true;
	private boolean isFocus = false;
	private Context mContext;

	@Override
	protected void onFocusChanged(boolean gainFocus, int direction,
			Rect previouslyFocusedRect) {
		isFocus = gainFocus;
		invalidate();
		super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
	}

	public void setVisibleArrow(boolean bl) {
		mVisibleArrow = bl;
	}

	public WheelSpinnerView(Context context) {
		super(context);
		mContext = context;
		setVisibleItems(1);
		setOrientation(LinearLayout.HORIZONTAL);
	}

	/**
	 * Constructor
	 */
	public WheelSpinnerView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
		setVisibleItems(1);
		setOrientation(LinearLayout.HORIZONTAL);
	}

	public void setAdapterData(String[] type) {
		mType = type;
		setViewAdapter(new SpinnerAdapter(mContext, mType));
	}

	public String[] getAdapterData() {
		return mType;
	}

	/**
	 * Constructor
	 */
	public WheelSpinnerView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		setVisibleItems(1);
		setOrientation(LinearLayout.HORIZONTAL);
	}

	class SpinnerAdapter extends AbstractWheelTextAdapter {
		ArrayList<String> securityList;

		protected SpinnerAdapter(Context context, String[] type) {
			super(context, R.layout.wheel_spinner_item, R.id.spinner_item);
			securityList = new ArrayList<String>(Arrays.asList(type));
		}

		@Override
		public int getItemsCount() {
			if (securityList != null) {
				return securityList.size();
			}
			return 0;
		}

		@Override
		protected CharSequence getItemText(int index) {
			return securityList.get(index);
		}

		@Override
		public View getItem(int index, View convertView, ViewGroup parent) {
			View view = super.getItem(index, convertView, parent);
			return view;
		}

	}

	public int getSelectedItemPosition() {
		return getCurrentItem();
	}

	public String getSelectedItem() {
		int index = getCurrentItem();
		if (mType != null && mType.length > index && index >= 0) {
			return mType[index];
		}
		return "";
	}

	@Override
	protected void drawCenterRect(Canvas canvas) {
		if (mVisibleArrow) {
			int center = getHeight() / 2;
			int dOffset = (int) (50 / 1.5 / 2);
			mLeftDrawable.setBounds(0, center - dOffset, (int) (23 / 1.5),
					center + dOffset);
			mLeftDrawable.draw(canvas);
			mRightDrawable.setBounds(getWidth() - (int) (23 / 1.5), center
					- dOffset, getWidth(), center + dOffset);
			mRightDrawable.draw(canvas);
		}
		if (isFocus) {
			setBackgroundResource(R.drawable.list_switch_select);
		} else {
			setBackgroundColor(0x005f5f5f);
		}
	}
}
